package com.metis.document.parse.dialog.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@SuppressWarnings("unused")
public class Gongshi {

    private final static Logger logger = LoggerFactory.getLogger(Gongshi.class);

    /**
     * ×,
     */
    public static String letters = "∅,⊆,⊇,∈,∏,←,∑,↑,⌒,→,↓,Γ,Δ,⊕,↖,↗,↘,Θ,⊙,↙,√,Λ,∝,∞,Ξ,∟,∠,Π,Σ,⊥,∥,Φ,∧,§,Χ,∨,Ψ,∩,Ω,∪,∫,∮,±,α,β,△,γ,δ,∴,ε,∵,ζ,∶,η,∷,θ,ι,κ,λ,μ,∽,ν,ξ,ο,π,ρ,σ,τ,⋅,υ,φ,χ,≈,ψ,ω,◊,≌,≒,≠,≡,≤,≥,≦,≧,≮,≯,÷";

    public static Pattern pattern = Pattern.compile(String.format("[%s]", letters.replaceAll(",", "")));

    public static char defaultEndLetter = '。';

    public static Integer defaultGSNum = 5;

    public static Gongshi gongshi = new Gongshi();

    private Gongshi(){}

    public String replaceGongShi(String content) {
        if (isExistsGongShi(content)) {
            Position iqr = findIqr(content);
            if (null != iqr) {
                String find = findGongShi(content, iqr);
                logger.info(find);
                StringBuilder sb = new StringBuilder(content);
                return sb.delete(iqr.getQ1(), iqr.getQ3()).toString();
            } else {
                // 整个content 删除
                return "";
            }
        }
        return content;
    }

    public Position findIqr(String content) {
        List<Integer> positions = findIndexGongShi(content);
        positions.sort(Comparator.comparingInt(o -> o));
        if (positions.size() >= defaultGSNum) {
            int q1 = positions.get(0);
            int q3 = positions.get(positions.size() - 1);

            int leftIdx = findFirstPeriodFromLeft(content, q1);
            int rightIdx = findFirstPeriodFromRight(content, q3);
            return new Position(leftIdx, rightIdx);
        }
        return null;
    }


    public String findGongShi(String content, Position iqr) {
        if (isExistsGongShi(content)) {
            return content.substring(iqr.getQ1(), iqr.getQ3());
        }
        return null;
    }

    /**
     * 如果段落中包含下列其中一项 则认为该 content 包含公式
     *
     * @param content content
     * @return boolean
     */
    public Boolean isExistsGongShi(String content) {
        Matcher matcher = pattern.matcher(content);
        int cn = 0;
        while(matcher.find()){
            cn++;
        }
        System.out.println(cn);
        return cn > 0;
    }

    /**
     * 在 content 中找到每个公式符号 对于的位置
     *
     * @param content content
     * @return list int
     */
    public List<Integer> findIndexGongShi(String content) {
        List<Integer> gongShiIndex = new ArrayList<>();
        for (int i = 0; i < content.length(); i++) {
            char c = content.charAt(i);
            if (letters.contains(String.valueOf(c))) {
                gongShiIndex.add(i);
            }
        }
        return gongShiIndex;
    }


    // 从位置a向左查找第一个句号的下标
    public static int findFirstPeriodFromLeft(String str, int a) {
        for (int i = a; i >= 0; i--) {
            if (str.charAt(i) == defaultEndLetter) {
                return i;
            }
        }
        return 0;
    }

    // 从位置b向右查找第一个句号的下标
    public static int findFirstPeriodFromRight(String str, int b) {
        for (int i = b; i < str.length(); i++) {
            if (str.charAt(i) == defaultEndLetter) {
                return i;
            }
        }
        return str.length();
    }

    protected static class Position {
        int q1;
        int q3;

        Position(Integer q1, Integer q3) {
            this.q1 = q1;
            this.q3 = q3;
        }

        public int getQ1() {
            return q1;
        }

        public int getQ3() {
            return q3;
        }
    }

}
